package com.fourth.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fourth.dto.*;
import com.fourth.dto.base.PageResult;
import com.fourth.dto.base.ResultBase;
import com.fourth.service.DatabaseManageService;
import com.fourth.utils.BizAssertUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * 数据源信息表 前端控制器
 * </p>
 *
 * @author cfy
 * @since 2023-02-27
 */
@RestController
@RequestMapping("/database/manage")
@Api(value = "DatabaseManageController", tags = "数据工厂")
public class DatabaseManageController {

    @Autowired
    private DatabaseManageService databaseManageService;

    /**
     * 添加数据源接口
     *
     * @param addOrUpdateDatabaseDto
     * @return ResultBase<Void>
     */
    @ApiOperation(value = "添加数据源接口", notes = "添加数据源接口")
    @PostMapping("/add")
    public ResultBase<Void> add(@Validated @RequestBody AddOrUpdateDatabaseDto addOrUpdateDatabaseDto) {

        // 新增不需要id
        addOrUpdateDatabaseDto.setId(null);
        // 失败
        BizAssertUtils.isTrue(databaseManageService.addOrUpdate(addOrUpdateDatabaseDto),
                "添加数据源失败");
        // 成功
        return ResultBase.success("添加数据源成功");
    }

    /**
     * 修改数据源接口
     *
     * @param addOrUpdateDatabaseDto
     * @return ResultBase<Void>
     */
    @ApiOperation(value = "修改数据源接口", notes = "修改数据源接口")
    @PostMapping("/update")
    public ResultBase<Void> update(@Validated @RequestBody AddOrUpdateDatabaseDto addOrUpdateDatabaseDto) {

        // 检查是否有id
        BizAssertUtils.isTrue(addOrUpdateDatabaseDto.getId() != null
                , "数据源id不能为空");
        BizAssertUtils.isTrue(addOrUpdateDatabaseDto.getId() != 0
                , "数据源id不能0");

        // 失败
        BizAssertUtils.isTrue(databaseManageService.addOrUpdate(addOrUpdateDatabaseDto),
                "修改数据源失败");
        // 成功
        return ResultBase.success("修改数据源成功");
    }

    /**
     * 删除数据源接口
     *
     * @param deleteDto
     * @return
     */
    @ApiOperation(value = "删除数据源接口", notes = "删除数据源接口")
    @PostMapping("/delete")
    public ResultBase<Void> delete(@RequestBody DeleteDto deleteDto) {
        Integer flag = databaseManageService.delete(deleteDto);
        if (flag.equals(1)) {
            return ResultBase.success("删除成功");
        } else {
            return ResultBase.fail("删除失败");
        }
    }

    /**
     * 修改应用状态接口
     *
     * @param changeApplyStatusDto
     * @return
     */
    @ApiOperation(value = "修改应用状态接口", notes = "修改应用状态接口,应用状态=='applyStatus'，0为停用状态，1为发布状态，2为未发布状态")
    @PostMapping("/change")
    public ResultBase<Void> changeApplyStatus(@RequestBody @Validated ChangeApplyStatusDto changeApplyStatusDto) {
        Integer flag = databaseManageService.changeApplyStatus(changeApplyStatusDto);
        if (flag == 1) {
            return ResultBase.success("修改成功");
        } else {
            return ResultBase.fail("修改失败");
        }
    }


    @GetMapping("/getDatabase/{id}")
    @ApiOperation(value = "根据id查询基本数据源信息", notes = "")
    public ResultBase<DatabaseDto> getDatabaseById(@PathVariable("id") Integer id) {
        DatabaseDto databaseById = databaseManageService.getDatabaseById(id);
        return ResultBase.successData(databaseById);
    }

    @PostMapping("/test")
    @ApiOperation(value = "测试连接")
    public ResultBase<Void> testConnection(@RequestBody @Validated TestConnectionDto testConnectionDto) {
        databaseManageService.connectionDataSource(testConnectionDto);
        return ResultBase.success("连通测试成功！");
    }

    @ApiOperation(value = "分页查询数据源列表信息", notes = "应用状态，'applyStatus'，0为停用状态，1为发布状态，2为未发布状态；sort，更新时间排序：0为正序，1为倒序")
    @PostMapping("/query")
    public PageResult<QueryListDto> query(@RequestBody @Validated QueryDatabaseDto queryDatabaseDto) {
        Page<QueryListDto> dataDtoPage = databaseManageService.queryDatabase(queryDatabaseDto);
        return PageResult.from(dataDtoPage);
    }


}

